#!/usr/bin/env bash
# ~/.bashrc by kenny.wang

shopt -s checkwinsize
case "$TERM" in
  xterm-color|*-256color) color_prompt=yes;;
esac
[ "$TERM" == "tmux-256color" ] && export TERM=xterm-256color

if [ -x /usr/bin/dircolors ]; then
  test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
  alias ls='ls --color=auto'
  alias grep='grep --color=auto'
  alias fgrep='fgrep --color=auto'
  alias egrep='egrep --color=auto'
fi

function get-host-tag {
  [ -z $HOST_TAG ] && printf "(\e[31mNULL\e[m)" || printf "(\e[34m$HOST_TAG\e[m)"
}

# User specific aliases and functions
function get-ps {
  local errorlevel=$?
  [ -e .git ] && printf "\e[32m(git)\e[m "
  if [ "$errorlevel" != "0" ];then
    printf "[$(date "+%T")] C:\e[31m%s\e[m" $errorlevel
  else
    printf "[$(date "+%T")]"
  fi
}

if [[ $(whoami) == "root" ]];then
  if [ -z $HOST_TAG ]; then
    PS1="\n\[\033[1;34m\]#\[\033[m\] \[\033[30;43m\]\u\[\033[m\] \[\033[0;37m\]@\[\033[m\] \[\033[0;32m\]${HOSTNAME%.*}\[\033[m\].\[\033[0;35m\]${HOSTNAME##*.}\[\033[m\](\[\e[31m\]NULL\[\e[m\]) \[\033[0;37m\]in\[\033[m\] \[\033[0;93m\]\w\[\033[m\] \$(get-ps)\n\[\033[1;31m\]\$\[\033[0m\] "
  else
    PS1="\n\[\033[1;34m\]#\[\033[m\] \[\033[30;43m\]\u\[\033[m\] \[\033[0;37m\]@\[\033[m\] \[\033[0;32m\]${HOSTNAME%.*}\[\033[m\].\[\033[0;35m\]${HOSTNAME##*.}\[\033[m\](\[\e[34m\]${HOST_TAG}\[\e[m\]) \[\033[0;37m\]in\[\033[m\] \[\033[0;93m\]\w\[\033[m\] \$(get-ps)\n\[\033[1;31m\]\$\[\033[0m\] "
  fi
else
  if [ -z $HOST_TAG ]; then
    PS1="\n\[\033[1;34m\]#\[\033[m\] \[\033[00;36m\]\u\[\033[m\] \[\033[0;37m\]@\[\033[m\] \[\033[0;32m\]${HOSTNAME%.*}\[\033[m\].\[\033[0;35m\]${HOSTNAME##*.}\[\033[m\](\[\e[31m\]NULL\[\e[m\]) \[\033[0;37m\]in\[\033[m\] \[\033[0;93m\]\w\[\033[m\] \$(get-ps)\n\[\033[1;31m\]\$\[\033[0m\] "
  else
    PS1="\n\[\033[1;34m\]#\[\033[m\] \[\033[00;36m\]\u\[\033[m\] \[\033[0;37m\]@\[\033[m\] \[\033[0;32m\]${HOSTNAME%.*}\[\033[m\].\[\033[0;35m\]${HOSTNAME##*.}\[\033[m\](\[\e[34m\]${HOST_TAG}\[\e[m\]) \[\033[0;37m\]in\[\033[m\] \[\033[0;93m\]\w\[\033[m\] \$(get-ps)\n\[\033[1;31m\]\$\[\033[0m\] "
  fi
fi

OS_ID=$(grep -E '^ID=' /etc/os-release 2>/dev/null | awk -F'=' '{gsub(/"/, "", $2); print $2}')

hash bind 2>/dev/null
if [ "$?" == "0" ];then
  # check line editing
  if [[ $(set -o | grep ^emacs | grep on) != "" ]];then
    #[ "$OS_ID" == "ubuntu" ] && bind -x '"\C-l":"clear&&. last-ps1"'
    bind '"\e\e":"\C-asudo \C-e"'
    bind '"\e-":"\C-a\e[C\e[C\e[C\e[C\e[C\C-u\C-e"'
  fi
fi

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

[ -n $TARGET_DIR ] && [ -e ~/$TARGET_DIR ] && cd ~/$TARGET_DIR || mkdir -p ~/$TARGET_DIR && cd ~/$TARGET_DIR

[ ! -e ~/$TARGET_DIR.ssh ] && ln -s ~/.ssh ~/$TARGET_DIR.ssh

[ -f ~/$TARGET_DIR.env ]   && source ~/$TARGET_DIR.env   || cp ~/$TARGET_DIR.shell/_env   ~/$TARGET_DIR.env   && source ~/$TARGET_DIR.env
[ -f ~/$TARGET_DIR.alias ] && source ~/$TARGET_DIR.alias || cp ~/$TARGET_DIR.shell/_alias ~/$TARGET_DIR.alias && source ~/$TARGET_DIR.alias
